<?php

namespace app\adminapi\controller\v1\order;

use app\adminapi\controller\AuthController;
use app\Request;
use app\services\order\TurntableLogServices;
use app\services\order\TurntableServices;
use app\services\user\UserServices;
use think\facade\App;

/**
 * 订单管理
 * Class StoreOrder
 * @package app\adminapi\controller\v1\order
 */
class Turntable extends AuthController
{
    protected $services;

    protected $turntableLog;

    /**
     * StoreOrder constructor.
     * @param App $app
     * @param TurntableServices $service
     * @param TurntableLogServices $logServices
     */
    public function __construct(App $app, TurntableServices $service, TurntableLogServices $logServices)
    {
        parent::__construct($app);
        $this->services = $service;
        $this->turntableLog = $logServices;
    }

    public function list()
    {
        return app('json')->success($this->services->getList());
    }

    public function edit(int $id)
    {
        return $this->success($this->services->getOne(['id' => $id]));
    }

    public function update(int $id)
    {
        $post = $this->request->postMore([
            ['name', ''],
            ['balance', 0],
            ['percent', 0],
            ['image', ''],
        ]);
        $this->services->update($id, $post);
        return $this->success('保存奖品成功!');
    }

    public function turntableLog(Request $request)
    {
        $get = $this->request->getMore([
            ['account', ''],
            ['prize', 0],
        ]);
        /** @var UserServices $userServices */
        $userServices = app()->make(UserServices::class);
        $where = [];
        if (!empty($get['account'])) {
            $user = $userServices->getOne(['account|phone' => $get['account']]);
            $where['uid'] = $user['uid'];
        }
        if (!empty($get['prize'])) {
            $where['prize'] = $get['prize'];
        }
        $turntables = $this->services->getList();
        $turntable = array_column($turntables, 'name', 'id');
        $balance = array_column($turntables, 'balance', 'id');

        $list = $this->turntableLog->getList($where);
        $count = $this->turntableLog->getCount($where);

        if (empty($where['prize'])) {
            $sum = [];
            foreach ($balance as $prizeId => $worth) {
                $where['prize'] = $prizeId;
                $sum[$prizeId] = $this->turntableLog->getCount($where) * $worth;
            }
            $total = array_sum($sum);
        } else {
            $total = $count * $balance[$where['prize']];
        }

        $points = intval($count) * 100;

        $uIds = array_column($list, 'uid');
        $users = $userServices->getUsers($uIds);
        $user = array_column($users, 'nickname', 'uid');
        foreach ($list as &$item) {
            $item['prizeName'] = $turntable[$item['prize']];
            $item['nickname'] = $user[$item['uid']];
        }
        return $this->success(compact('list', 'count', 'total', 'points'));
    }


}
